﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml; 
using System.Reflection.Metadata;
using System.Text.RegularExpressions;
using System.Web;
using Ws.Util;
using Ws.dto;
using Ws.Client;

namespace WsSample
{
    /// <summary>
    /// 调用示例
    /// </summary>
    public class WsClientSample
    {

        #region 单例
        private static WsClientSample instance;
        public static WsClientSample Instance
        {
            get
            {
                instance ??= new WsClientSample();
                return instance;
            }
        }
        #endregion

        // add  lock
        private static object _lock = new();//  锁
        private static bool isStart = false;
        private static readonly int IntervalLoop = 10 * 1000; // 毫秒

        private readonly string TestUrl = "http://133.124.45.162:8080/services/FSUService";


        public WsClientSample()
        {
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            isStart = true;
            CheckLoop();
        }

        /// <summary>
        /// 循环处理自动巡检任务
        /// </summary>
        private void CheckLoop()
        {
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Begin……");
                while (isStart)
                {
                    lock (_lock)
                    {
                        GetDataAsync();
                    }
                    Thread.Sleep(IntervalLoop); // 一秒钟执行一次
                }
                Console.WriteLine("End……");
            });
        }

        /// <summary>
        /// 调用Get_Data，传入参数。
        /// </summary> 
        private async Task GetDataAsync()
        {
            for (int i=0;i<1000;i++)
            {
                string fsuId = "21202208310001";
                List<string> deviceIdList = new() { "070200000000001" };
                List<string> signalIdList = new() { };
                
                string xmlData = WsUtils.CreateArgs4WS(fsuId, deviceIdList, signalIdList);
                string result = await new WsClient(TestUrl).Request(xmlData);
                DealResult(result);
            }
        }

        /// <summary>
        /// 处理FSU返回的数据，解析成对象
        /// </summary>
        private async Task DealResult(string result)
        {
            try
            {
                // 解析xml  修改bean泛型
                XmlSerializer x = new(typeof(ResponseCommon<ResponseInfo4MonitorData>));
                StringReader reader = new(result);
                XmlReader xmlReader = new XmlTextReader(reader);
                ResponseCommon<ResponseInfo4MonitorData> respObj = (ResponseCommon<ResponseInfo4MonitorData>)x.Deserialize(xmlReader);

                // 处理
                ResponseInfo4MonitorData info = respObj!.Info;
                string fsuId = info.FSUID;
                string result1 = info.Result; // 0 失败，1 成功
                if (result1 == "0")
                {
                    Console.WriteLine("请求失败原因 ： " + info.FailureCause);
                }
                else
                {
                    // 每个Device含：ID 和 TSemaphore
                    List<ResponseDevice4MonitorData> deviceArray = info.Values.DeviceList.Device;

                    // 这里是：当前FSU的 所有请求的设备 所有请求的信元的 数据
                    foreach (ResponseDevice4MonitorData deviceData in deviceArray)
                    {
                        var devId = deviceData.IDAttribute; // 设备ID
                        List<TSemaphore4MonitorData> TSemList = deviceData.TSemaphore;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DealResult()异常 : {0} ", ex.Message);
            }

        }

    }

}
